#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define RECEV_IP "255.255.255.255"
#define RECEV_PORT "7777"
#define ACIP_FILE "/tmp/acip"
int main(int argc,char *argv[])
{
	int socketfd, i = 0;
	socklen_t addr_len;
	char buf[64];
	struct sockaddr_in server_addr;
	const char *m_command = "kill `ps |grep \"dhcpcd_daemon.sh\"|grep -v \"grep\"|awk -F ' ' '{print $1}'` && killall sleep && dhcpcd_daemon.sh &";
	if((socketfd = socket(PF_INET,SOCK_DGRAM,0)) < 0)
	{
		perror("socket");
		exit(-1);
	}
	memset(&server_addr,0,sizeof(server_addr));
	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = inet_addr(RECEV_IP);
	server_addr.sin_port = htons(atoi(RECEV_PORT));    
	addr_len=sizeof(server_addr);

	if(bind(socketfd,(struct sockaddr*)&server_addr,addr_len) < 0)
	{
		perror("bind");
		exit(-1);
	}
	while(1)
	{
		memset(buf, '\0', 64);
		if(recvfrom(socketfd,buf,64,0,(struct sockaddr*)&server_addr,&addr_len) < 0)
		{
			perror("sendrto");
			exit(-1);
		}
		printf("from: %s port:%d >%s\n",inet_ntoa(server_addr.sin_addr),ntohs(server_addr.sin_port),buf);
		if(strstr(buf, "AWCXWITHIMACTOASPMD") )
		{
			if(-1 == access(ACIP_FILE, 0))
			{
				system(m_command);
			}
		}
	}

	close(socketfd);
	return 0;
}
